// sm2_gmssl_fixed.c
#include <stdio.h>
#include <string.h>
#include <gmssl/sm2.h>
#include <gmssl/rand.h>

#define SM2_NUMWORD 32

void print_hex(const char* label, const unsigned char* data, size_t len) {
    printf("%s: ", label);
    for (size_t i = 0; i < len; i++) {
        printf("%02x", data[i]);
    }
    printf("\n");
}

void print_message(const char* label, const unsigned char* data, size_t len) {
    printf("%s: ", label);
    for (size_t i = 0; i < len; i++) {
        printf("%c", data[i]);
    }
    printf(" (");
    for (size_t i = 0; i < len; i++) {
        printf("%02x", data[i]);
    }
    printf(")\n");
}

int SM2_KeyGen_Encrypt_Decrypt_Test() {
    SM2_KEY key;
    unsigned char msg[] = "Hello SM2 Test Message";
    unsigned char ciphertext[512];
    unsigned char decrypted[512];
    size_t ciphertext_len, decrypted_len;
    int i;
    
    printf("=== SM2 Key Generation + Encryption + Decryption Test ===\n\n");
    
    // 1. 生成SM2密钥对
    printf("1. Generating SM2 key pair...\n");
    if (sm2_key_generate(&key) != 1) {
        printf("❌ SM2 key generation failed\n");
        return 1;
    }
    printf("✅ SM2 key pair generated successfully\n\n");
    
    // 2. 显示测试消息
    printf("2. Test message:\n");
    print_message("   Original", msg, strlen((char*)msg));
    printf("   Message length: %zu bytes\n\n", strlen((char*)msg));
    
    // 3. 加密
    printf("3. Encrypting message...\n");
    if (sm2_encrypt(&key, msg, strlen((char*)msg), ciphertext, &ciphertext_len) != 1) {
        printf("❌ SM2 encryption failed\n");
        return 1;
    }
    printf("✅ Encryption successful\n");
    printf("   Ciphertext length: %zu bytes\n", ciphertext_len);
    print_hex("   Ciphertext", ciphertext, ciphertext_len);
    printf("\n");
    
    // 4. 解密
    printf("4. Decrypting ciphertext...\n");
    if (sm2_decrypt(&key, ciphertext, ciphertext_len, decrypted, &decrypted_len) != 1) {
        printf("❌ SM2 decryption failed\n");
        return 1;
    }
    decrypted[decrypted_len] = '\0';
    print_message("   Decrypted", decrypted, decrypted_len);
    printf("\n");
    
    // 5. 验证
    printf("5. Verification...\n");
    if (memcmp(msg, decrypted, strlen((char*)msg)) == 0 && decrypted_len == strlen((char*)msg)) {
        printf("✅ SM2 Test: PASS - Encryption/Decryption successful\n");
        printf("✅ Message correctly encrypted and decrypted\n");
    } else {
        printf("❌ SM2 Test: FAIL - Message mismatch\n");
        return 1;
    }
    
    return 0;
}

int SM2_Multiple_Tests() {
    SM2_KEY key;
    int i;
    
    printf("\n=== SM2 Multiple Message Test ===\n");
    
    // 生成密钥对
    if (sm2_key_generate(&key) != 1) {
        printf("Key generation failed\n");
        return 1;
    }
    
    // 测试多个不同长度的消息
    const char* test_messages[] = {
        "A",
        "Hello",
        "Hello SM2",
        "This is a longer test message for SM2 encryption",
        "1234567890!@#$%^&*()"
    };
    
    int num_tests = sizeof(test_messages) / sizeof(test_messages[0]);
    int passed_tests = 0;
    
    for (i = 0; i < num_tests; i++) {
        const char* msg = test_messages[i];
        size_t msg_len = strlen(msg);
        unsigned char ciphertext[512];
        unsigned char decrypted[512];
        size_t ciphertext_len, decrypted_len;
        
        printf("\nTest %d/%d: ", i+1, num_tests);
        printf("'%s' (%zu bytes)\n", msg, msg_len);
        
        // 加密
        if (sm2_encrypt(&key, (unsigned char*)msg, msg_len, ciphertext, &ciphertext_len) != 1) {
            printf("  ❌ Encryption failed\n");
            continue;
        }
        
        // 解密
        if (sm2_decrypt(&key, ciphertext, ciphertext_len, decrypted, &decrypted_len) != 1) {
            printf("  ❌ Decryption failed\n");
            continue;
        }
        
        decrypted[decrypted_len] = '\0';
        
        // 验证
        if (memcmp(msg, decrypted, msg_len) == 0 && decrypted_len == msg_len) {
            printf("  ✅ PASS\n");
            passed_tests++;
        } else {
            printf("  ❌ FAIL - Message mismatch\n");
        }
    }
    
    printf("\nMultiple Test Results: %d/%d tests passed\n", passed_tests, num_tests);
    
    if (passed_tests == num_tests) {
        printf("✅ All multiple tests PASSED\n");
        return 0;
    } else {
        printf("❌ Some tests FAILED\n");
        return 1;
    }
}

int main() {
    printf("SM2 Algorithm Comprehensive Test\n");
    printf("================================\n\n");
    
    // 测试1: 基本加解密
    if (SM2_KeyGen_Encrypt_Decrypt_Test() != 0) {
        printf("\n❌ Basic SM2 test failed\n");
        return 1;
    }
    
    // 测试2: 多消息测试
    if (SM2_Multiple_Tests() != 0) {
        printf("\n❌ Multiple message test failed\n");
        return 1;
    }
    
    printf("\n🎉 ALL SM2 TESTS COMPLETED SUCCESSFULLY!\n");
    printf("✅ SM2 algorithm is working correctly\n");
    printf("✅ Ready for GmSSL command line verification\n");
    
    printf("\nNext steps:\n");
    printf("1. Use 'gmssl sm2keygen' to generate keys for verification\n");
    printf("2. Use 'gmssl sm2encrypt' and 'gmssl sm2decrypt' for command line verification\n");
    printf("3. Compare results with our implementation\n");
    
    return 0;
}
